//两数相加
//https://leetcode.cn/problems/add-two-numbers/
public class Test {
    public static void main(String[] args) {
        //
    }
}

class ListNode {
     int val;
     ListNode next;
     ListNode() {}
     ListNode(int val) { this.val = val; }
     ListNode(int val, ListNode next) { this.val = val; this.next = next; }
}
class Solution {
    public ListNode addTwoNumbers(ListNode l1, ListNode l2) {
        //
        ListNode head = new ListNode();
        ListNode cur = head;
        ListNode cur1 = l1;
        ListNode cur2 = l2;
        int t = 0;//记录每一位的两数相加
        while(cur1!=null&&cur2!=null){
            //
            t += cur1.val+cur2.val;
            cur.next = new ListNode(t%10);
            cur = cur.next;
            t = t/10;
            cur1 = cur1.next;
            cur2 = cur2.next;
        }

        while(cur1!=null){
            t += cur1.val;
            cur.next = new ListNode(t%10);
            cur = cur.next;
            t = t/10;
            cur1 = cur1.next;
        }
        while(cur2!=null){
            t += cur2.val;
            cur.next = new ListNode(t%10);
            cur = cur.next;
            t = t/10;
            cur2 = cur2.next;
        }
        if(t!=0){
            cur.next = new ListNode(t%10);
        }
        return head.next;

    }
}